
package com.tsingtree.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsingtree.base.auth.context.LoginContextHolder;
import com.tsingtree.base.log.BussinessLog;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.trees.core.base.controller.BaseController;
import com.tsingtree.trees.core.reqres.response.ResponseData;
import com.tsingtree.trees.core.util.ToolUtil;
import com.tsingtree.trees.kernel.model.exception.ServiceException;
import com.tsingtree.sys.core.constant.dictmap.DeleteDict;
import com.tsingtree.sys.core.constant.dictmap.NoticeMap;
import com.tsingtree.sys.core.constant.factory.ConstantFactory;
import com.tsingtree.sys.core.exception.enums.BizExceptionEnum;
import com.tsingtree.sys.core.log.LogObjectHolder;
import com.tsingtree.sys.modular.system.entity.Notice;
import com.tsingtree.sys.modular.system.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 通知控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController {

    private String PREFIX = "/modular/system/notice/";

    @Autowired
    private NoticeService noticeService;

    /**
     * 跳转到通知列表首页
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "notice.html";
    }

    /**
     * 跳转到添加通知
     *
     * @author jagger
     */
    @RequestMapping("/notice_add")
    public String noticeAdd() {
        return PREFIX + "notice_add.html";
    }

    /**
     * 跳转到修改通知
     *
     * @author jagger
     */
    @RequestMapping("/notice_update")
    public String noticeUpdate() {
        return PREFIX + "notice_edit.html";
    }

    /**
     * 获取通知详情
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData noticeDetail(Long noticeId) {
        Notice notice = this.noticeService.getById(noticeId);
        Map<String, Object> noticeMap = BeanUtil.beanToMap(notice);
        return ResponseData.success(noticeMap);
    }

    /**
     * 获取通知列表
     *
     * @author jagger
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<Notice> list = this.noticeService.list(condition);
        return LayuiPageFactory.createPageInfo(list);
    }

    @RequestMapping("/chooseUser")
    public String spec() {
        return PREFIX + "choose_user.html";
    }

    /**
     * 新增通知
     *
     * @author jagger
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增通知", key = "title", dict = NoticeMap.class)
    public Object add(Notice notice) {
        if (ToolUtil.isOneEmpty(notice, notice.getTitle(), notice.getContent())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        notice.setCreateUser(LoginContextHolder.getContext().getUserId());
        notice.setCreateTime(LocalDateTime.now());
        this.noticeService.save(notice);
        return SUCCESS_TIP;
    }

    /**
     * 删除通知
     *
     * @author jagger
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除通知", key = "noticeId", dict = DeleteDict.class)
    public Object delete(@RequestParam Long noticeId) {

        //缓存通知名称
        LogObjectHolder.me().set(ConstantFactory.me().getNoticeTitle(noticeId));

        this.noticeService.removeById(noticeId);

        return SUCCESS_TIP;
    }

    /**
     * 修改通知
     *
     * @author jagger
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改通知", key = "title", dict = NoticeMap.class)
    public Object update(Notice notice) {
        if (ToolUtil.isOneEmpty(notice, notice.getNoticeId(), notice.getTitle(), notice.getContent())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Notice old = this.noticeService.getById(notice.getNoticeId());
        old.setTitle(notice.getTitle());
        old.setContent(notice.getContent());
        this.noticeService.updateById(old);
        return SUCCESS_TIP;
    }

}
